/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaPrincipal.java
 *
 * Created on 11/02/2011, 10:23:41 AM
 */
package com.py.mbaez.mbpgsql.gui;

import com.py.mbaez.mbpgsql.util.AddTabbedHeader;
import com.py.mbaez.mbpgsql.util.PgSession;
import com.py.mbaez.mbpgsql.util.ClosableTabbedHeader;
import javax.swing.*;
import jsyntaxpane.DefaultSyntaxKit;

/**
 *
 * @author mbaez
 */
public class VentanaPrincipal extends javax.swing.JFrame {

    /** Creates new form VentanaPrincipal */
    public VentanaPrincipal() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            DefaultSyntaxKit.initKit();
        } catch (Exception e) {
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toolBar = new javax.swing.JToolBar();
        ejecutarButton = new javax.swing.JButton();
        pararButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        guardarButton = new javax.swing.JButton();
        sqlQueryResultTabbedPane = new javax.swing.JTabbedPane();
        menuBar = new javax.swing.JMenuBar();
        inicioMenu = new javax.swing.JMenu();
        conexionMenuItem = new javax.swing.JMenuItem();
        removeTabMenuItem = new javax.swing.JMenuItem();
        guardarResultadoMenuItem = new javax.swing.JMenuItem();
        salirMenuItem = new javax.swing.JMenuItem();
        procesosMenu = new javax.swing.JMenu();
        ejecutarMenuItem = new javax.swing.JMenuItem();
        pararMenuItem = new javax.swing.JMenuItem();
        guardarMenuItem = new javax.swing.JMenuItem();
        ayudaMenu = new javax.swing.JMenu();
        acercaMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mbpgsql");

        toolBar.setFloatable(false);
        toolBar.setRollover(true);

        ejecutarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/exaile.png"))); // NOI18N
        ejecutarButton.setToolTipText("Ejecutar");
        ejecutarButton.setFocusable(false);
        ejecutarButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        ejecutarButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ejecutarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ejecutarButtonActionPerformed(evt);
            }
        });
        toolBar.add(ejecutarButton);

        pararButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/agt_action_fail.png"))); // NOI18N
        pararButton.setToolTipText("Parar");
        pararButton.setFocusable(false);
        pararButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        pararButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        pararButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pararButtonActionPerformed(evt);
            }
        });
        toolBar.add(pararButton);
        toolBar.add(jSeparator2);

        guardarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/media-floppy.png"))); // NOI18N
        guardarButton.setFocusable(false);
        guardarButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        guardarButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        guardarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarButtonActionPerformed(evt);
            }
        });
        toolBar.add(guardarButton);

        sqlQueryResultTabbedPane.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        inicioMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/home.png"))); // NOI18N

        conexionMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        conexionMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/db_add.png"))); // NOI18N
        conexionMenuItem.setText("Nueva Conexion");
        conexionMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conexionMenuItemActionPerformed(evt);
            }
        });
        inicioMenu.add(conexionMenuItem);

        removeTabMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        removeTabMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/db_remove.png"))); // NOI18N
        removeTabMenuItem.setText("Eliminar Conexion");
        removeTabMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeTabMenuItemActionPerformed(evt);
            }
        });
        inicioMenu.add(removeTabMenuItem);

        guardarResultadoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        guardarResultadoMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/media-floppy.png"))); // NOI18N
        guardarResultadoMenuItem.setText("Guardar Resultado");
        guardarResultadoMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarResultadoMenuItemActionPerformed(evt);
            }
        });
        inicioMenu.add(guardarResultadoMenuItem);

        salirMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        salirMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/exit.png"))); // NOI18N
        salirMenuItem.setText("Salir");
        salirMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirMenuItemActionPerformed(evt);
            }
        });
        inicioMenu.add(salirMenuItem);

        menuBar.add(inicioMenu);

        procesosMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/advancedsettings.png"))); // NOI18N

        ejecutarMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        ejecutarMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/exaile.png"))); // NOI18N
        ejecutarMenuItem.setText("Ejecutar");
        ejecutarMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ejecutarMenuItemActionPerformed(evt);
            }
        });
        procesosMenu.add(ejecutarMenuItem);

        pararMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
        pararMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/agt_action_fail.png"))); // NOI18N
        pararMenuItem.setText("Parar");
        procesosMenu.add(pararMenuItem);

        guardarMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        guardarMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/media-floppy.png"))); // NOI18N
        guardarMenuItem.setText("Guardar ");
        guardarMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarMenuItemActionPerformed(evt);
            }
        });
        procesosMenu.add(guardarMenuItem);

        menuBar.add(procesosMenu);

        ayudaMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/access.png"))); // NOI18N

        acercaMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        acercaMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/py/mbaez/mbpgsql/icon/info.png"))); // NOI18N
        acercaMenuItem.setText("Acerca de...");
        acercaMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acercaMenuItemActionPerformed(evt);
            }
        });
        ayudaMenu.add(acercaMenuItem);

        menuBar.add(ayudaMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 568, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(sqlQueryResultTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sqlQueryResultTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ejecutarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ejecutarButtonActionPerformed
        SqlQueryResult panel = (SqlQueryResult) this.sqlQueryResultTabbedPane.getSelectedComponent();
        panel.ejecutarLocalQuery();
    }//GEN-LAST:event_ejecutarButtonActionPerformed

    private void conexionMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conexionMenuItemActionPerformed
        ConexionDialog conexionDialog = new ConexionDialog(this, true);
        conexionDialog.setVisible(true);
    }//GEN-LAST:event_conexionMenuItemActionPerformed

    private void guardarResultadoMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarResultadoMenuItemActionPerformed
        SqlQueryResult panel = (SqlQueryResult) this.sqlQueryResultTabbedPane.getSelectedComponent();
        panel.guardarLocalResult();
    }//GEN-LAST:event_guardarResultadoMenuItemActionPerformed

    private void ejecutarMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ejecutarMenuItemActionPerformed
        this.ejecutarButtonActionPerformed(evt);
    }//GEN-LAST:event_ejecutarMenuItemActionPerformed

    private void removeTabMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeTabMenuItemActionPerformed
        int tabIndex = sqlQueryResultTabbedPane.getSelectedIndex();
        sqlQueryResultTabbedPane.remove(tabIndex);
        PgSession.CURRENTCONEXION.remove(tabIndex);
        if (PgSession.CURRENTCONEXION.size() == 0) {
            sqlQueryResultTabbedPane.remove(0);
        }
    }//GEN-LAST:event_removeTabMenuItemActionPerformed

    private void pararButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pararButtonActionPerformed
        SqlQueryResult panel = (SqlQueryResult) this.sqlQueryResultTabbedPane.getSelectedComponent();
        panel.cancelarLocalQuery();
    }//GEN-LAST:event_pararButtonActionPerformed

    private void salirMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirMenuItemActionPerformed
        this.dispose();
    }//GEN-LAST:event_salirMenuItemActionPerformed

    private void guardarMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarMenuItemActionPerformed
        SqlQueryResult panel = (SqlQueryResult) this.sqlQueryResultTabbedPane.getSelectedComponent();
        panel.guardarLocalQuery();
    }//GEN-LAST:event_guardarMenuItemActionPerformed

    private void acercaMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acercaMenuItemActionPerformed
        JDialog dialog=new AcercaDialog(this,true);
        dialog.setVisible(true);
    }//GEN-LAST:event_acercaMenuItemActionPerformed

    private void guardarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarButtonActionPerformed
        guardarMenuItemActionPerformed(null);
    }//GEN-LAST:event_guardarButtonActionPerformed

    protected void addSqlQueryResultTab() {
        int connexionIndex = PgSession.CURRENTCONEXION.size() - 1;
        if (connexionIndex == 0) {
            JPanel empty = new JPanel();
            sqlQueryResultTabbedPane.add(empty, 0);
            sqlQueryResultTabbedPane.setTabComponentAt(0, new AddTabbedHeader(sqlQueryResultTabbedPane, this));
        }

        String title = PgSession.SERVER;
        title += ":" + PgSession.DATABASE;
        SqlQueryResult tab = new SqlQueryResult(connexionIndex);
        sqlQueryResultTabbedPane.add(tab, connexionIndex);
        sqlQueryResultTabbedPane.setTitleAt(connexionIndex, title);
        sqlQueryResultTabbedPane.setTabComponentAt(connexionIndex, new ClosableTabbedHeader(sqlQueryResultTabbedPane));
        sqlQueryResultTabbedPane.setSelectedIndex(connexionIndex);


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem acercaMenuItem;
    private javax.swing.JMenu ayudaMenu;
    private javax.swing.JMenuItem conexionMenuItem;
    private javax.swing.JButton ejecutarButton;
    private javax.swing.JMenuItem ejecutarMenuItem;
    private javax.swing.JButton guardarButton;
    private javax.swing.JMenuItem guardarMenuItem;
    private javax.swing.JMenuItem guardarResultadoMenuItem;
    private javax.swing.JMenu inicioMenu;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton pararButton;
    private javax.swing.JMenuItem pararMenuItem;
    private javax.swing.JMenu procesosMenu;
    private javax.swing.JMenuItem removeTabMenuItem;
    private javax.swing.JMenuItem salirMenuItem;
    private javax.swing.JTabbedPane sqlQueryResultTabbedPane;
    private javax.swing.JToolBar toolBar;
    // End of variables declaration//GEN-END:variables
}
